package com.xy.p2p.action.front;



import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import com.xy.common.page.Pagination;
import com.xy.common.security.encoder.PwdEncoder;
import com.xy.common.util.DictUtils;
import com.xy.common.util.StrUtils;
import com.xy.common.web.CookieUtils;
import com.xy.core.web.WebErrors;
import com.xy.p2p.model.Tmember;
import com.xy.p2p.model.Tmemberassetaccount;
import com.xy.p2p.model.Twidrapp;
import com.xy.p2p.service.TmemberService;
import com.xy.p2p.service.TwidrappService;



@Controller
public class TwidrappAction{
	private static final Logger log = LoggerFactory.getLogger(TwidrappAction.class);
	public TwidrappAction() {
	}
	@Autowired
	private PwdEncoder pwdEncoder;
 
 
 	/**
	 * 提现申请表新增页面
	 * 
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping("v_twidrappAdd.do")
	public String add(HttpServletRequest request, ModelMap model) {
		model.addAttribute("twidrapp", new Twidrapp());
        setDict(model);
		return "twidrappEdit";
	}

	/**
	 * 提现申请表新增保存ajax
	 * 
	 * @param twidrapp
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping("/member/o_twidrappSave.jspx")
	public void save(Twidrapp twidrapp, HttpServletRequest request, ModelMap model, HttpServletResponse response) {
//		WebErrors errors = validateSave(twidrapp, request);
		String msg = "";
//		if (errors.hasErrors()) {
//			return;
//			// return errors.showErrorPage(model);
//		}
		try {
			response.setContentType("text/Xml;charset=gbk");
			PrintWriter out = response.getWriter();
			Tmember tmember = (Tmember) request.getSession().getAttribute(TmemberAction.AUTH_KEY);
			if (null == tmember.getWithdrawpassword()) {
				msg = "请设置提现密码...";
				out.println(msg);
				return ;
			}
			if (!tmember.getWithdrawpassword().equals(pwdEncoder.encodePassword((twidrapp.getTmember().getWithdrawpassword())))) {
				msg = "提现密码不正确...";
				out.println(msg);
				return ;
			}
			twidrapp.setTmember(tmember);
			msg = twidrappService.widrapp(twidrapp);
			out.println(msg);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	@RequestMapping("/member/o_twidrappSave2.jspx")
	public String save2(Twidrapp twidrapp, HttpServletRequest request, ModelMap model, HttpServletResponse response) {
//		WebErrors errors = validateSave(twidrapp, request);
		String msg = "";
//		if (errors.hasErrors()) {
//			return;
//			// return errors.showErrorPage(model);
//		}
		try {
			Tmember temp = (Tmember) request.getSession().getAttribute(TmemberAction.AUTH_KEY);
			Tmember tmember = tmemberService.get(temp.getMemberid());
			if (null == tmember.getWithdrawpassword()) {
				msg = "请设置提现密码...";
				model.addAttribute("msg", msg);
				return "forward:/member/v_tmemberassetaccountToWidrapp.jspx";
			}
			if (!tmember.getWithdrawpassword().equals(pwdEncoder.encodePassword((twidrapp.getTmember().getWithdrawpassword())))) {
				msg = "提现密码不正确...";
				model.addAttribute("msg", msg);
				return "forward:/member/v_tmemberassetaccountToWidrapp.jspx";
			}
			twidrapp.setTmember(tmember);
			msg = twidrappService.widrapp(twidrapp);
			model.addAttribute("msg", msg);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "forward:/member/v_tmemberassetaccountToWidrapp.jspx";
	}

	/**
	 * 提现申请表删除
	 * 
	 * @param ids
	 * @return
	 */
	@RequestMapping("o_twidrappDelete.do")
	public String delete(long[] ids) {
		if (ids != null) {
			for (long id : ids) {
				twidrappService.remove(id);
			}
		}
		return "redirect:v_twidrappList.do";
	}

	/**
	 * 提现申请表编辑页面
	 * 
	 * @param waid
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping("v_twidrappEdit.do")
	public String edit(long waid, HttpServletRequest request, ModelMap model) {
		Twidrapp twidrapp = twidrappService.get(waid);
		model.addAttribute("twidrapp", twidrapp);
        setDict(model);
		return "twidrappEdit";
	}

	/**
	 * 提现申请表详情
	 * 
	 * @param waid
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping("v_twidrappDetail.do")
	public String detail(long waid, HttpServletRequest request, ModelMap model) {
		Twidrapp twidrapp = twidrappService.get(waid);
		model.addAttribute("twidrapp", twidrapp);
        setDict(model);
		return "twidrappView";
	}

	/**
	 * 提现申请表查询（分页）
	 * 
	 * @param twidrapp
	 * @param pageNo
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping("/member/v_twidrappList.jspx")
	public String listPage(Twidrapp twidrapp, Integer pageNo,
			HttpServletRequest request, ModelMap model) {
		if (pageNo == null)
			pageNo = 1;
		twidrapp.setTmember((Tmember)request.getSession().getAttribute(TmemberAction.AUTH_KEY));
		Pagination pagination = twidrappService.getTwidrappByPage(toMap(twidrapp),
				pageNo, CookieUtils.getPageSize(request));
		model.addAttribute("pagination", pagination);
        setDict(model);
        //用于导航栏
        model.addAttribute("dis", 8);
		return "/front/member/twidrappList.html";
	}

	/**
	 * 提现申请表查询
	 * 
	 * @param twidrapp
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping("v_twidrappList1.do")
	public String list(Twidrapp twidrapp, HttpServletRequest request, ModelMap model) {
		List<Twidrapp> list = twidrappService.getTwidrapp(toMap(twidrapp));
		model.addAttribute("list", list);
        setDict(model);
		return "twidrappList";
	}


 	/***
	 * 将页面输入信息转成map
	 * 
	 * @param twidrapp
	 * @return
	 */	private Map<String,Object> toMap(Twidrapp twidrapp){
		Map<String,Object> map=new HashMap<String, Object>();
      		if(twidrapp.getWaid()!=null&&!twidrapp.getWaid().equals("")){
			map.put("waid", twidrapp.getWaid());
		}
		if(twidrapp.getTmember()!=null&&!twidrapp.getTmember().equals("")){
			map.put("Tmember", twidrapp.getTmember());
		}
		if(twidrapp.getWdam()!=null&&!twidrapp.getWdam().equals("")){
			map.put("wdam", twidrapp.getWdam());
		}
		if(twidrapp.getWdamdate()!=null&&!twidrapp.getWdamdate().equals("")){
			map.put("wdamdate", twidrapp.getWdamdate());
		}
		if(twidrapp.getTuser()!=null&&!twidrapp.getTuser().equals("")){
			map.put("Tuser", twidrapp.getTuser());
		}
		if(twidrapp.getRevieweddate()!=null&&!twidrapp.getRevieweddate().equals("")){
			map.put("revieweddate", twidrapp.getRevieweddate());
		}
		if(twidrapp.getOperator()!=null&&!twidrapp.getOperator().equals("")){
			map.put("operator", twidrapp.getOperator());
		}
		if(twidrapp.getOperatedate()!=null&&!twidrapp.getOperatedate().equals("")){
			map.put("operatedate", twidrapp.getOperatedate());
		}
		if(twidrapp.getStatus()!=null&&!twidrapp.getStatus().equals("")){
			map.put("status", twidrapp.getStatus());
		}
		if(twidrapp.getRemark()!=null&&!twidrapp.getRemark().equals("")){
			map.put("remark", twidrapp.getRemark());
		}      
		return map;
	}
   
	/**
	 * 设置页面所需的数据字典
	 * @param model
	 */
	private void setDict(ModelMap model){
      model.addAttribute("statusMap", DictUtils.getDictByType(DictUtils.TWIDRAPP_STATUS));		
	}

	/**
	 * 数据校验
	 * @param model
	 */
	private WebErrors validateSave(Twidrapp twidrapp, HttpServletRequest request) {
		WebErrors errors = WebErrors.create(request);
		/**if (errors.ifOutOfLength(username, "username", 1, 100)) {
			return errors;
		}  */    
		return errors;
	}

   

	@Resource
	private TwidrappService twidrappService;  
	@Resource
	private TmemberService tmemberService;  
}